package com.soo.hoo.admin.api;

import cn.hutool.core.bean.BeanUtil;
import com.soo.hoo.admin.model.dto.FileDTO;
import com.soo.hoo.admin.model.dto.ImageFileDTO;
import com.soo.hoo.admin.model.dto.UserDTO;
import com.soo.hoo.admin.model.entity.SysUser;
import com.soo.hoo.admin.model.vo.UserVO;
import com.soo.hoo.admin.service.FileService;
import com.soo.hoo.admin.service.UserService;
import com.soo.hoo.admin.utils.SaLoginUtil;
import com.soo.hoo.core.annotation.OperateLog;
import com.soo.hoo.core.constant.Constants;
import com.soo.hoo.core.enums.BusinessTypeEnum;
import com.soo.hoo.core.exception.GlobalException;
import com.soo.hoo.core.model.Page;
import com.soo.hoo.core.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 用户相关
 * </p>
 *
 * @author 杨燚
 * @since 2022/2/18 17:18
 */
@Tag(name = "用户管理")
@RequestMapping("/user/")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserController {

    private final UserService userService;
    private final FileService fileService;

    @Operation(summary = "登录")
    @PostMapping("login")
    @OperateLog(title = "登录日志", businessType = BusinessTypeEnum.ENTRY)
    public Result<UserVO> login(@RequestBody @Valid UserDTO user) {
        return Result.success("登录成功", userService.login(user));
    }

    @Operation(summary = "校验token")
    @GetMapping("checkToken")
    public Result<Void> checkToken(@RequestParam String token) {
        userService.checkToken(token);
        return Result.success();
    }

    @Operation(summary = "退出登录")
    @PostMapping("logout")
    public Result<Void> logout(@RequestBody @Valid UserVO param) {
        userService.logout(param.getId());
        return Result.success("退出成功");
    }

    @Operation(summary = "获取用户信息")
    @PostMapping("userInfo")
    public Result<UserVO> getUserInfo(@RequestBody @Valid UserVO param) {
        return Result.success(userService.getUserInfo(param.getId()));
    }

    @Operation(summary = "分页")
    @PostMapping("page")
    public Result<Page<SysUser>> page(@RequestBody @Valid Page<SysUser> param) {
        Page<SysUser> page = userService.page(param);
        return Result.success(page);
    }

    @Operation(summary = "新建用户")
    @PostMapping("create")
    public Result<Void> create(@RequestBody @Valid UserVO param) {
        SysUser user = new SysUser();
        BeanUtil.copyProperties(param, user);
        user.setPassword(Constants.DEFAULT_PASSWORD);
        userService.save(user);
        return Result.success();
    }

    @Operation(summary = "编辑用户")
    @PostMapping("update")
    public Result<Void> update(@RequestBody @Valid UserVO param) {
        if (param.getId() == null) {
            throw new GlobalException("编辑失败：【编辑时id字段不能为空】");
        }
        SysUser byId = userService.getById(param.getId());
        if (byId.getUsername().equals(Constants.ADMIN)) {
            throw new GlobalException("超级管理员不支持编辑操作");
        }
        if (!byId.getUsername().equals(param.getUsername())) {
            userService.verifyUser(param.getUsername());
        }
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(param, sysUser);
        sysUser.setAvatarUrl(param.getAvatar());
        sysUser.setPassword(byId.getPassword());
        userService.updateById(sysUser);
        return Result.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("delete")
    public Result<Void> delete(@RequestParam Long id) {
        SysUser user = userService.getById(id);
        if (user.getUsername().equals(Constants.ADMIN)) {
            throw new GlobalException("超级管理员不支持删除操作");
        }
        userService.removeById(id);
        return Result.success();
    }

    @PostMapping("uploadAvatar")
    @Operation(summary = "上传头像")
    public Result<ImageFileDTO> uploadAvatar(@RequestParam MultipartFile file, @RequestParam(required = false) String module) {
        UserVO user = SaLoginUtil.getUser();
        if (user == null) {
            throw new GlobalException("当前登录用户出错，请联系管理员");
        }
        ImageFileDTO fileInfo = fileService.uploadImage(file, module == null ? "avatar" : module);
        user.setAvatar(fileInfo.getImagePath());
        this.update(user);
        return Result.success("上传成功", fileInfo);
    }
}
